﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using System.Web;
using com.dz.ht.common;

namespace com.dz.ht.components
{
    public class CollectBookReviewManager
    {
        /// <summary>
        /// 百度首页编码
        /// </summary>
        Encoding m_encoding = Encoding.GetEncoding("gbk");
        string m_search_url = "http://tieba.baidu.com/f?kw={0}";
        string m_reg_review_title="<td[^<>]*?class=[\'\"]*?thread_title[\'\"]*?>\\s*?<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>";
        string m_reg_review_content = "<cc>(.*?)</cc>";
        DataManger m_data_mgr = new DataManger();
        /// <summary>
        /// 触发发送消息的事件
        /// </summary>
        public event SendMsgEventHandler SendMsg;


        /// <summary>
        /// 发送消息到客户端
        /// </summary>
        /// <param name="i_msg"></param>
        private void SendMsg2Client(string i_msg)
        {
            SendMsgEventArgs e = new SendMsgEventArgs();
            e.Msg = i_msg;

            if (SendMsg != null)
            {
                SendMsg(this, e);
            }
        }

        public void StartCollectReview()
        {
         
            DataSet t_ds = m_data_mgr.GetBookByReview(2167);

            long t_book_id = 0;

            string t_url = string.Empty;

            string t_html = string.Empty;

            for (int index = 0; index < t_ds.Tables[0].Rows.Count; index++)
            {
                if (!long.TryParse(t_ds.Tables[0].Rows[index]["bookId"].ToString(), out t_book_id))
                    t_book_id = 0;

                t_url = string.Format(m_search_url, HttpUtility.UrlEncode(t_ds.Tables[0].Rows[index]["bookName"].ToString(), m_encoding));

                t_html = NetSiteCatchManager.ReadUrl(t_url,m_encoding);

                CollectReview(t_html, t_book_id);
            }
            t_ds = null;
            //2 小时
            System.Threading.Thread.Sleep(2 * 60 * 60 * 1000);
        }

        /// <summary>
        /// 收集
        /// </summary>
        /// <param name="i_html"></param>
        /// <param name="i_book_id"></param>
        private void CollectReview(string i_html, long i_book_id)
        {
            MatchCollection t_ma = Regex.Matches(i_html, m_reg_review_title, RegexOptions.IgnoreCase | RegexOptions.Compiled);

            string t_url = string.Empty;

            string t_html = string.Empty;

            if (t_ma != null && t_ma.Count > 0)
            {
                foreach (Match t_mc in t_ma)
                {
                    if (t_mc.Groups.Count > 1)
                    {
                        t_url = string.Format("http://tieba.baidu.com{0}", t_mc.Groups[1].Value);
                        t_html = NetSiteCatchManager.ReadUrl(t_url, m_encoding);

                        Review2DB(t_html, i_book_id);
                    }
                }
            }
        }

        private void Review2DB(string i_html, long i_book_id)
        {
            MatchCollection t_ma = Regex.Matches(i_html, m_reg_review_content, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            string t_review_content = string.Empty;

            if (t_ma != null && t_ma.Count > 0)
            {
                foreach (Match t_mc in t_ma)
                {
                    if (t_mc.Groups.Count > 0)
                    {
                        t_review_content = t_mc.Groups[1].Value.ToString();
                        t_review_content = FilterContent(t_review_content);
                        SendMsg2Client("获得评论：" + t_review_content);

                        if (t_review_content.Length > 10)
                        {
                            m_data_mgr.InsertBookReview(i_book_id, t_review_content);
                        }
                    }
                }
            }

        }

        private string FilterContent(string i_content)
        {
            if (string.IsNullOrEmpty(i_content))
                return i_content;
            i_content = Regex.Replace(i_content, "<img.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_content = Regex.Replace(i_content, "<a.*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_content=Regex.Replace(i_content,"\\d+楼","",RegexOptions.IgnoreCase|RegexOptions.Compiled);
            return i_content;
        }
    }
}
